package com.jushu.storbox.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jushu.storbox.R;
import com.jushu.storbox.entities.UploadSelectItem;
import com.jushu.storbox.util.PublicTools;

public class UploadSelectTypeAdapter extends BaseAdapter {
	/** context */
	private Context context;

	/** layout inflater */
	private LayoutInflater mInflater;

	/** handler to refresh ui */
	private Handler handler;

	/** cloud drive uplaod select item file list */
	private ArrayList<UploadSelectItem> uploadSelectItemList;

	/**
	 * UploadSelectTypeAdapter Controller function
	 * 
	 * @param context
	 *            Context
	 * @param uploadSelectItemList
	 *            ArrayList<UploadSelectItem> uplaod select item file list data
	 * @param handler
	 *            Handler refresh ui
	 */
	public UploadSelectTypeAdapter(Context context, ArrayList<UploadSelectItem> uploadSelectItemList, Handler handler) {
		// Auto-generated constructor stub
		this.context = context;
		this.handler = handler;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.uploadSelectItemList = uploadSelectItemList;
	}

	/**
	 * UploadSelectTypeAdapter getCount
	 */
	public int getCount() {

		return uploadSelectItemList.size();
	}

	/**
	 * UploadSelectTypeAdapter getItem
	 * 
	 * @param position
	 *            int
	 * @return UploadSelectItem filelist get position
	 */
	public UploadSelectItem getItem(int position) {

		return uploadSelectItemList.get(position);
	}

	/**
	 * UploadSelectTypeAdapter getItemId
	 * 
	 * @param position
	 *            int
	 * @return long filelist get position
	 */
	public long getItemId(int position) {

		return position;
	}

	/**
	 * UploadSelectTypeAdapter getView
	 * 
	 * @param position
	 *            int
	 * @param convertView
	 *            View
	 * @param parent
	 *            ViewGroup
	 * @return View ListAdapter item view
	 */
	public View getView(final int position, View convertView, ViewGroup parent) {

		final int clickPosition = position;
		ViewHolder holder = new ViewHolder();
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.item_more_fileselect, null);
			holder.imageView = (ImageView) convertView.findViewById(R.id.more_fileselect_item_im);
			holder.textView = (TextView) convertView.findViewById(R.id.more_fileselect_item_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// init data
		final UploadSelectItem uploadSelectItem = uploadSelectItemList.get(clickPosition);
		if ("createFiel".equals(uploadSelectItem.getId())) {
			holder.imageView.setBackgroundResource(R.mipmap.upload_createfile);
		} else if ("photo".equals(uploadSelectItem.getId())) {
			holder.imageView.setBackgroundResource(R.mipmap.upload_photo);

		} else if ("document".equals(uploadSelectItem.getId())) {
			holder.imageView.setBackgroundResource(R.mipmap.upload_document);

		}
		// else if ("video".equals(uploadSelectItem.getId())) {
		// holder.imageView.setBackgroundResource(R.drawable.upload_);
		//
		// }
		else if ("AllFile".equals(uploadSelectItem.getId())) {
			holder.imageView.setBackgroundResource(R.mipmap.upload_allfile);

		}
		if (PublicTools.isLocaleLanguageCN()) {
			holder.textView.setText(uploadSelectItem.getItemNameCn());
		} else {
			holder.textView.setText(uploadSelectItem.getItemNameEn());
		}
		convertView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if ("createFiel".equals(uploadSelectItem.getId())) {
					Message message = new Message();
					message.what = 0;
					handler.sendMessage(message);
					
				} else if ("photo".equals(uploadSelectItem.getId())) {
					Message message = new Message();
					message.what = 1;
					handler.sendMessage(message);
				} else if ("document".equals(uploadSelectItem.getId())) {
					Message message = new Message();
					message.what = 2;
					handler.sendMessage(message);
				}
				// else if ("video".equals(uploadSelectItem.getId())) {
				// holder.imageView.setBackgroundResource(R.drawable.upload_);
				// Message message = new Message();
				// message.what = 0;
				// handler.sendMessage(message);
				// }
				else if ("AllFile".equals(uploadSelectItem.getId())) {
					Message message = new Message();
					message.what = 3;
					handler.sendMessage(message);
				}
			}
		});
		return convertView;
	}

	/**
	 * ViewHolder adapter data class
	 */
	private class ViewHolder {
		public ImageView imageView;
		public TextView textView;
		public String id;
	}

}
